//
//  JYYBViewController.m
//  何五路
//
//  Created by _ADY on 15/8/20.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "JYYBViewController.h"

@interface JYYBViewController ()

@end

@implementation JYYBViewController
@synthesize mTableView,JYybArray;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = bgColor;
    
    HightInt = 30;
    sizeInt = 20;
    if ([self.title  isEqualToString:TKjyb]) {
        HightInt = 45;
        sizeInt = 10;
    }
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, self.view.bounds.size.width, self.view.bounds.size.height-70)];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];
    
    __weak __typeof(self) weakSelf = self;
    
    mTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    mTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    pricString = nil;
    NSMutableAttributedString *str= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"我的%@:",self.title]];
    fLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, screenMySize.size.width, 40)];
    fLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:labelSize+3];
    fLabel.textColor = [UIColor colorWithRed:114/255.0 green:113/255.0 blue:113/255.0 alpha:1];
    fLabel.attributedText = str;
    fLabel.backgroundColor = bgColor;
    [self.view addSubview:fLabel];
    
    
    UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(0, 40+64, screenMySize.size.width, 30)];
    mView.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    [self.view addSubview:mView];
    
    NSArray *mArray = [ NSArray arrayWithObjects:@"来源/用途",[NSString stringWithFormat:@"%@变化",self.title],@"日期", nil];
    if ([self.title isEqualToString:TKfl]) {
        mArray = [ NSArray arrayWithObjects:@"被推荐人",@"金元宝变化",@"日期", nil];
    }
    for (int i = 0; i < 3; i ++)
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenMySize.size.width*i/3, 0, screenMySize.size.width/3, 30)];
        titleLabel.text = [mArray objectAtIndex:i];
        titleLabel.textAlignment = 1;
        titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
        titleLabel.textColor = [UIColor colorWithRed:74/255.0 green:74/255.0 blue:74/255.0 alpha:1];
        [mView addSubview:titleLabel];
    }
    
   [mTableView.header beginRefreshing];
}

- (void)loadNewData
{
    pageForNumber = 1;
    [self upDateTableView];
}
- (void)loadMoreData
{
    pageForNumber ++;
    [self upDateTableView];
}

-(void)upDateTableView
{
    if ((pageForNumber-1)*sizeInt <= [JYybArray count]||pageForNumber ==1)
    {
        NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
         if ([self.title isEqualToString:TKfl])
         {
             [postDic setObject:[NSNumber numberWithInt:pageForNumber] forKey:@"command.PageNumber"];
             [postDic setObject:[NSNumber numberWithInt:sizeInt] forKey:@"command.PageSize"];
         }
        else
        {
            [postDic setObject:[NSNumber numberWithInt:pageForNumber] forKey:@"pageNumber"];
            [postDic setObject:[NSNumber numberWithInt:sizeInt] forKey:@"pageSize"];
        }

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            NSDictionary *ret = nil;
            if ([self.title isEqualToString:TKjyb]) {
                ret = [SendService MyGoldIngot:postDic];
            }
            else if ([self.title isEqualToString:TKyyb])
                ret = [SendService MySilverIngot:postDic];
            else
                ret = [SendService CustomerReferralFeeRecordList:postDic];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSLog(@"ret %@",ret);
                if (ret !=nil)
                {

                    if (![[NSString stringWithFormat:@"%@",[ret objectForKey:@"Entity"]] isEqualToString:@"<null>"]) {
                        
                        if (pageForNumber == 1)
                        {
                            if (JYybArray != nil)
                            {
                                [JYybArray removeAllObjects];
                                JYybArray = nil;
                            }
                            JYybArray = [[NSMutableArray alloc] init];
                            
                            NSString *mStr = [NSString stringWithFormat:@"我的%@:",self.title];
                            if (![self.title isEqualToString:TKfl])
                            pricString = [NSString stringWithFormat:@"%@",[[ret objectForKey:@"Entity"] objectForKey:@"totalSilver"]];
                            else
                              pricString = [NSString stringWithFormat:@"%@",[[ret objectForKey:@"Entity"] objectForKey:@"TotalReferralFee"]];
                            NSMutableAttributedString *str= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",mStr,pricString]];
                            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:236/255.0 green:84/255.0 blue:96/255.0 alpha:1]  range:NSMakeRange(mStr.length,pricString.length)];
                            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:labelSize+3] range:NSMakeRange(mStr.length,pricString.length)];
                            fLabel.attributedText = str;
                        }
                        
                        for (NSDictionary *pic in [[ret objectForKey:@"Entity"] objectForKey:@"Items"])
                        {
                            [JYybArray addObject:pic];
                        }
                        
                    }

                }
                if (pageForNumber ==1)
                    [mTableView.header endRefreshing];
                else
                    [mTableView.footer endRefreshing];
                [mTableView reloadData];
                
            });
        });
        
    }
    else
    {
        [mTableView.footer noticeNoMoreData];
        [mTableView reloadData];
    }
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return JYybArray.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return HightInt;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"JYYB";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        aTableView.showsVerticalScrollIndicator = NO;
    }

    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    cell.backgroundColor = [UIColor clearColor];
    return [self mCell:cell cCTableView:aTableView cellForRowAtIndexPath:indexPath];
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell cCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dicS = [JYybArray objectAtIndex:indexPath.row];
//    NSLog(@"dicS %@",dicS);

    NSString *timeStr = nil;
    NSString *messageStr = nil;
    NSString *pointStr = nil;
    if ([self.title isEqualToString:TKfl])
    {
        timeStr = [NSString stringWithFormat:@"%@",[dicS objectForKey:@"CreateTime"]];
        messageStr = [NSString stringWithFormat:@"%@",[dicS objectForKey:@"Referral"]];
        pointStr = [NSString stringWithFormat:@"+%@",[dicS objectForKey:@"ReferralFee"]];
        
    }
    else
    {
        timeStr = [NSString stringWithFormat:@"%@",[dicS objectForKey:@"CreatedOnUtc"]];
        messageStr = [NSString stringWithFormat:@"%@",[dicS objectForKey:@"Message"]];
        pointStr = [NSString stringWithFormat:@"%@",[dicS objectForKey:@"Points"]];
    }
    [cell.contentView addSubview:[self setFrame:CGRectMake(0, 0, self.view.bounds.size.width*3/8, HightInt) setTitle:messageStr setColor:[UIColor colorWithRed:114/255.0 green:113/255.0 blue:113/255.0 alpha:1]]];

    UIColor *mColor = [UIColor colorWithRed:236/255.0 green:84/255.0 blue:96/255.0 alpha:1];
    NSArray *controls = [pointStr componentsSeparatedByString:@"+"];
    if (controls.count>1) {
        mColor = [UIColor colorWithRed:109/255.0 green:165/255.0 blue:74/255.0 alpha:1];
    }
    [cell.contentView addSubview:[self setFrame:CGRectMake(self.view.bounds.size.width*3/8, 0, self.view.bounds.size.width*2/8, HightInt) setTitle:pointStr  setColor:mColor]];
    

    [cell.contentView addSubview:[self setFrame:CGRectMake(self.view.bounds.size.width*5/8, 0, self.view.bounds.size.width*3/8, HightInt) setTitle:timeStr setColor:[UIColor colorWithRed:114/255.0 green:113/255.0 blue:113/255.0 alpha:1]]];

    
    return cell;
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setColor:(UIColor*)myColor
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 1;
    if ([self.title  isEqualToString:TKjyb]) {
        titleLabel.numberOfLines = 3;
    }
    else
        titleLabel.numberOfLines = 2;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-3];
    titleLabel.textColor = myColor;
    return titleLabel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
